import { _decorator, Component, Graphics, Node, UITransform, Color, Vec3, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AttackRangeIndicator')
export class AttackRangeIndicator extends Component {
    @property(Graphics)
    graphics: Graphics = null;

    @property
    pulseSpeed: number = 1;

    @property
    pulseScale: number = 0.1;

    @property
    defaultColor: Color = new Color(255, 0, 0, 80);

    @property
    warningColor: Color = new Color(255, 0, 0, 120);

    private _radius: number = 100;
    private _isWarning: boolean = false;
    private _currentAlpha: number = 80;
    private _pulseTween = null;

    init(radius: number) {
        this._radius = radius;
        this.node.setScale(1, 1, 1);
        
        // 确保有 UITransform 组件
        let transform = this.node.getComponent(UITransform);
        if (!transform) {
            transform = this.node.addComponent(UITransform);
        }
        transform.setContentSize(radius * 2, radius * 2);

        // 初始化Graphics
        if (!this.graphics) {
            this.graphics = this.node.getComponent(Graphics);
            if (!this.graphics) {
                this.graphics = this.node.addComponent(Graphics);
            }
        }

        this.startPulseAnimation();
        this.drawRange();
    }

    private drawRange() {
        this.graphics.clear();

        // 设置颜色
        const currentColor = this._isWarning ? this.warningColor : this.defaultColor;
        currentColor.a = this._currentAlpha;
        
        // 只绘制描边
        this.graphics.strokeColor = currentColor;
        this.graphics.lineWidth = 3; // 可以调整线条粗细

        // 绘制圆形
        this.graphics.circle(0, 0, this._radius);
        this.graphics.stroke();

        // 可选：绘制虚线效果
        this.graphics.strokeColor = new Color(255, 0, 0, this._currentAlpha * 0.5);
        this.graphics.lineWidth = 1;
        this.graphics.circle(0, 0, this._radius - 5); // 内圈
        this.graphics.stroke();
    }

    private startPulseAnimation() {
        // 停止现有动画
        if (this._pulseTween) {
            this._pulseTween.stop();
        }

        // 创建呼吸动画
        this._pulseTween = tween(this.node)
            .repeatForever(
                tween()
                    .to(this.pulseSpeed, { scale: new Vec3(1 + this.pulseScale, 1 + this.pulseScale, 1) })
                    .to(this.pulseSpeed, { scale: new Vec3(1, 1, 1) })
            )
            .start();
    }

    /**
     * 设置警告状态
     */
    public setWarning(isWarning: boolean) {
        if (this._isWarning === isWarning) return;
        
        this._isWarning = isWarning;
        this.drawRange();

        // 警告状态时加快脉动
        if (isWarning) {
            this.pulseSpeed = 0.5;
            this.pulseScale = 0.15;
        } else {
            this.pulseSpeed = 1;
            this.pulseScale = 0.1;
        }

        this.startPulseAnimation();
    }

    onDestroy() {
        if (this._pulseTween) {
            this._pulseTween.stop();
            this._pulseTween = null;
        }
    }
} 